import {
  listDatasourceInfo
} from '@/api/console/datasourceInfo'
import Vue from 'vue'

const basicData = [{
  label: '实例名称',
  dataName: 'dataSourceName',
  type: 'text',
  component: 'input',
  labelWidth: '100px'
}, {
  label: '驱动类',
  dataName: 'driver',
  type: 'text',
  component: 'input',
  labelWidth: '100px'
}, {
  label: 'IP',
  dataName: 'ip',
  type: 'text',
  component: 'input',
  labelWidth: '100px',
  rules: [
    {
      required: true,
      message: '请输入ip地址',
      trigger: 'change'
    },
    {
      pattern: /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
      message: '请输入正确的ip地址',
      trigger: 'change'
    }
  ]
},
{
  label: '端口',
  dataName: 'port',
  type: 'text',
  component: 'input',
  labelWidth: '100px'

},
{
  label: '用户',
  dataName: 'uname',
  type: 'text',
  component: 'input',
  labelWidth: '100px'
},
{
  label: '密码',
  dataName: 'pwd',
  type: 'password',
  component: 'input',
  labelWidth: '100px',
  rules: [{
    required: false,
    message: `请输入`,
    trigger: 'change'
  }]
}]

const seniorShowData = [{
  label: '自动提交',
  dataName: 'autoCommit',
  component: 'radio',
  dict: 'database_auto_commit',
  labelWidth: '100px'

}, {
  label: '连接超时',
  dataName: 'connectTimeout',
  component: 'input',
  type: 'number',
  onkeyup: "value=(value.replace(/\D/g,'')==''?'':parseInt(value))",
  labelWidth: '100px'

}, {
  label: '会话超时',
  dataName: 'socketTimeout',
  component: 'input',
  type: 'number',
  onkeyup: "value=(value.replace(/\D/g,'')==''?'':parseInt(value))",
  labelWidth: '100px'

}, {
  label: '编码格式',
  dataName: 'encoding',
  component: 'input',
  type: 'test',
  labelWidth: '100px'
}, {
  label: 'useUnicode',
  dataName: 'useUnicode',
  component: 'radio',
  labelWidth: '100px',
  option: () => {
    return [{
      label: '是',
      value: 'true'
    }, {
      label: '否',
      value: 'false'
    }, {
      label: '全不选',
      value: 'null'
    }]
  }
},
{
  label: '大小写状态',
  dataName: 'fieldCase',
  component: 'radio',
  dict: 'field_case',
  defaultValueDict: 'field_case',
  labelWidth: '100px'
},
{
  label: '标识符引号',
  dataName: 'quote',
  component: 'radio',
  dict: 'objectname_quote',
  defaultValueDict: 'objectname_quote',
  labelWidth: '100px'

}]

const seniorData = [{
  label: 'URL',
  dataName: 'url',
  component: 'input',
  type: 'text',
  span: 18,
  labelWidth: '100px'
}, {
  label: '备注',
  dataName: 'remark',
  component: 'input',
  type: 'textarea',
  span: 18,
  labelWidth: '100px',
  rules: [
    {
      required: false,
      message: `请输入备注`,
      trigger: 'change'
    }
  ]
}]

const mysql = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const dameng = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const oracle = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form, _data) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'serviceName',
      component: 'input',
      type: 'text',
      labelWidth: '100px',
      width: '70%',
      inlineEl: [
        {
          dataName: 'oracleType',
          component: 'select',
          width: '25%',
          labelWidth: '0px',
          defaultValue: 'serviceName',
          option() {
            return [
              {
                value: 'serviceName',
                label: '服务名'
              },
              {
                value: 'sid',
                label: 'sid'
              }
            ]
          },
          change(v, form) {
            Vue.set(form, v, '')
            return
          }
        }
      ]
    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const kingbase = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const sqlserver = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const shentong = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form, _data) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'serviceName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'
    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const gbase = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form, _data) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '实例名',
      dataName: 'GBASEDBTSERVER',
      component: 'input',
      type: 'text',
      labelWidth: '100px'
    },
    {
      label: '数据库',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const postgresql = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const db2 = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const tidb = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    {
      label: '实例名称',
      dataName: 'dataSourceName',
      type: 'text',
      component: 'input',
      labelWidth: '100px'
    }, {
      label: '驱动类',
      dataName: 'driver',
      type: 'text',
      component: 'input',
      labelWidth: '100px'
    }, {
      label: 'IP',
      dataName: 'ip',
      type: 'text',
      component: 'input',
      labelWidth: '100px',
      rules: [
        {
          required: true,
          message: '请输入ip地址',
          trigger: 'change'
        },
        {
          pattern: /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
          message: '请输入正确的ip地址',
          trigger: 'change'
        }
      ]
    },
    {
      label: '端口',
      dataName: 'port',
      type: 'text',
      component: 'input',
      labelWidth: '100px'
    },
    {
      label: '用户',
      dataName: 'uname',
      type: 'text',
      component: 'input',
      labelWidth: '100px'
    },
    {
      label: '密码',
      dataName: 'pwd',
      type: 'password',
      component: 'input',
      labelWidth: '100px',
      rules: [
        {
          required: false,
          message: '请输入密码',
          trigger: 'change'
        }
      ]
    },
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const opengauss = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    },
    {
      label: 'schema',
      dataName: 'schemaName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'

    }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

const oceanbase_mysql = [{
  titleData: {
    title: '基本信息',
    span: 13
  },
  gutter: 20,
  hederData: {
    span: 10,
    data: [{
      label: '加载模板',
      dataName: 'template',
      component: 'select',
      option: async function(type) {
        const arr = []
        const res = await listDatasourceInfo({
          type,
          dataType: 1
        })
        res.rows.forEach(f => {
          arr.push({
            label: f.dataSourceName,
            value: f.id,
            ...f
          })
        })
        return arr
      },
      change: async function(v, form) {
        let res = await listDatasourceInfo({
          dataType: 1
        })
        res = res.rows.find(f => f.id == v)
        delete res.id
        Object.keys(res).forEach(f => {
          Vue.set(form, f, String(res[f]))
        })
      }
    }]
  },
  judge(title) { return title == '添加数据源信息' },
  children: [
    ...basicData,
    {
      label: '数据库',
      dataName: 'databaseName',
      component: 'input',
      type: 'text',
      labelWidth: '100px'
    }
    // {
    //   label: '租户',
    //   dataName: 'tenant',
    //   component: 'input',
    //   type: 'text',
    //   labelWidth: '100px',
    //   rules: [
    //     {
    //       required: false,
    //       message: '请输入租户',
    //       trigger: 'change'
    //     }
    //   ]
    // }
  ]
},
{
  title: '高级配置',
  children: [
    ...seniorData
  ],
  showSpan: 12,
  showData: seniorShowData
}
]

export default {
  mysql,
  dameng,
  oracle,
  kingbase,
  sqlserver,
  shentong,
  gbase,
  postgresql,
  db2,
  tidb,
  opengauss,
  oceanbase_mysql
}
